package com.example.switchcontrolplatform.context;

import com.example.switchcontrolplatform.enumeration.ChannelType;
import com.example.switchcontrolplatform.item.PricingRequestMetadata;
import com.example.switchcontrolplatform.item.ProductItem;
import com.example.switchcontrolplatform.item.User;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 定价上下文 - 包含所有定价相关的信息
 * 企业级设计：包含完整的业务上下文信息
 */
public class PricingContext {
	private String transactionId; // 交易ID
	private User user; // 用户信息
	private List<ProductItem> productItems; // 商品列表
	private ChannelType channel; // 渠道
	private LocalDateTime transactionTime; // 交易时间
	private String currency; // 货币
	private Map<String, Object> additionalParams; // 额外的参数
	private PricingRequestMetadata metadata; // 请求元数据

	// 构造器
	public PricingContext(String transactionId, User user, List<ProductItem> productItems,
	                      ChannelType channel, LocalDateTime transactionTime, String currency) {
		this.transactionId = transactionId;
		this.user = user;
		this.productItems = productItems;
		this.channel = channel;
		this.transactionTime = transactionTime;
		this.currency = currency;
		this.metadata = new PricingRequestMetadata();
	}

	// Getters and Setters
	public String getTransactionId() { return transactionId; }
	public User getUser() { return user; }
	public List<ProductItem> getProductItems() { return productItems; }
	public ChannelType getChannel() { return channel; }
	public LocalDateTime getTransactionTime() { return transactionTime; }
	public String getCurrency() { return currency; }
	public Map<String, Object> getAdditionalParams() { return additionalParams; }
	public PricingRequestMetadata getMetadata() { return metadata; }

	public void setAdditionalParams(Map<String, Object> additionalParams) {
		this.additionalParams = additionalParams;
	}

	/**
	 * 计算商品总价
	 */
	public BigDecimal calculateOriginalTotal() {
		return productItems.stream()
				.map(item -> item.getUnitPrice().multiply(BigDecimal.valueOf(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);
	}

	/**
	 * 获取商品总数量
	 */
	public int getTotalQuantity() {
		return productItems.stream()
				.mapToInt(ProductItem::getQuantity)
				.sum();
	}
}
